using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;

namespace ComputerShopE3.Controllers
{
    public class CartController : Controller
    {
        //
        // GET: /Cart/
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();

        public ActionResult Index()
        {
            var cacgio = _db.GIO_HANG.Where(c => c.NGUOI_DUNG.TenDangNhap == User.Identity.Name);

            GIO_HANG giohang;

            if (cacgio.Count() == 0)
            {
                giohang = new GIO_HANG { NGUOI_DUNG = _db.NGUOI_DUNG.Where(c => c.TenDangNhap == User.Identity.Name).First() };
                _db.AddToGIO_HANG(giohang);
            }
            else
            {
                giohang = cacgio.First();
            }

            giohang.CHI_TIET_GIO_HANG.Load();

            long iTongTien = 0;

            foreach (var ctgiohang in giohang.CHI_TIET_GIO_HANG.ToList())
            {
                ctgiohang.SAN_PHAMReference.Load();
                ctgiohang.SAN_PHAM.LOAI_SAN_PHAMReference.Load();

                iTongTien += ctgiohang.SAN_PHAM.Gia;
            }

            ViewData["TongTien"] = iTongTien;

            return View(giohang.CHI_TIET_GIO_HANG.ToList());
        }

        //
        // GET: /Cart/Details/5
        //[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Details(/*FormCollection collection*/)
        {
            

            return View();
        }

        //
        // GET: /Cart/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Cart/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Cart/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Cart/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult AddToCart(int idSanPham, int SoLuong)
        {
            var cacgio = _db.GIO_HANG.Where(c => c.NGUOI_DUNG.TenDangNhap == User.Identity.Name);

            GIO_HANG giohang;
            if (cacgio.Count() == 0)
            {
                //GIO_HANG giohang;

                //if (cacgio.Count() == 0)
                //{
                giohang = new GIO_HANG { NGUOI_DUNG = _db.NGUOI_DUNG.Where(c => c.TenDangNhap == User.Identity.Name).First() };
                _db.AddToGIO_HANG(giohang);
            }
            else
            {
                giohang = cacgio.First();
            }
            //}
            //else
            //{
            //    giohang = cacgio.ToList()[0];

            //    foreach (var gh in cacgio)
            //    {
            //        if (giohang.CHI_TIET_GIO_HANG.First() > giohang
            //    }
            //}

            var sp = _db.SAN_PHAM.Where(c => c.id == idSanPham).First();

            CHI_TIET_GIO_HANG ctgiohang = new CHI_TIET_GIO_HANG
            {
                SAN_PHAM = sp,
                NgayCapNhat = DateTime.Now,
                NgayLapGio = DateTime.Now,
                NgayThanhToan = DateTime.Now,
                SoLuong = (short)SoLuong,
                GIO_HANG = giohang,
                TongTien = SoLuong * sp.Gia
            };

            _db.AddToCHI_TIET_GIO_HANG(ctgiohang);
            _db.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add update logic here
                _db.DeleteObject(_db.CHI_TIET_GIO_HANG.Where(c => c.id == id).First());
                _db.SaveChanges();
            }
            catch
            {
            }

            return RedirectToAction("Index");
        }
    }
}
